package content.views;
import content.views.links.BlocLinks;
import feffects.easing.Quad;
import feffects.easing.Sine;
import js.Lib;
import params.GlobalParams;
import tools.ASprite;
import tools.BoundProvider;

/**
 * ...
 * @author PRASS
 */

class Links extends BaseView
{
	//private var _nbinks		: Int;
	private var _links		: Array<Object>;
	private var _data 		: Object;
	private var _container	: ASprite;
	private var _title		: ASprite;
	private var _ul			: Array<BlocLinks>;
	
	private var _currentX	: Float;
	private var _currentY	: Float;
	
	public function new(id : String, tpl : String ) 
	{
		super(id, tpl);
		
		dom.css("background-image", "url(" + GlobalParams.BG_LINKS + ")");
		
		_ul 	= new Array<BlocLinks>();
		_links 	= new Array<Object>();
	}
	
	override public function build():Void 
	{
		_container = new ASprite( new JQuery("#linksContainer") );
		
		_title = new ASprite( new JQuery("#linksTitle") );
		_title.setAlpha(0);
		
		untyped $.getJSON( 'data/links.json', _onLinksLoaded);
	}
	
	private function _onLinksLoaded( obj : Object ) : Void
	{
		_data = obj;

		var i : Int = 0;
		//_nbinks = 0;
		
		// counter
		while ( i < 200 )
		{
			if ( _data.links.link[i] != null)
				_links.push(_data.links.link[i]);
			else
				break;
			
			i++;
		}
		
		i = 0;
		
		// build ul
		var nbBlocs : Int = cast Math.ceil(_links.length / 8);
		var flag : Bool = false;
		while ( i < nbBlocs )
		{
			var j : Int = i + 1;
			_container.dom.append('<ul id="col'+j+'"></ul>');
			
			_ul[i] = new BlocLinks( new JQuery("#col" + j) );
			_ul[i].setAlpha(0);
			
			flag = !flag;
			
			if ( flag )
				_ul[i].currentY = 100;
			else	
				_ul[i].currentY = -100;
				
			i++;
		}
		
		
		_container.setWidth( 160 * nbBlocs) ;
		_container.setHeight(150) ;
		
		i = 0;
		
		// build li
		while ( i < _links.length )
		{
			var str : String = '<li><a href="'+_links[i].url+'" target="_blank">'+_links[i].label+'</a></li>';
			
			_ul[ Math.floor(i / 8)].dom.append( str );
			
			i++;
		}
		
		
	}
	
	override public function play():Void 
	{
		super.play();
		
		_title.tween( 0, _title.setAlpha, 1, 800, Quad.easeOut, null, 500 );
		
		var i : Int = 0;
		
		while ( i < _ul.length )
		{
			_ul[i].tweens([ { prop:_ul[i].setY, init:_ul[i].currentY, value:0 }, { prop:_ul[i].setAlpha, init:0, value:1 } ], 500, Quad.easeInOut, null, i*150);
			i++;
		}

	}
	
	override public function hide():Void 
	{
		var i : Int = 0;
		
		while ( i < _ul.length )
		{
			_ul[i].tween(1, _ul[i].setAlpha, 0, 400, Quad.easeOut, null, i * 150);
			i++;
		}
		
		_title.tween( 1, _title.setAlpha, 0, 600, Quad.easeOut);
	}
	
	override public function resize(_w:Int, _h:Int):Void 
	{
		super.resize(_w, _h);
		
		if ( _container == null ) return;
			
		 if ( _w < 1100)
			_currentX	=  ( _w + 216 -  _container.getWidth() ) * .5;
		else
			_currentX	=  356;
			
		_currentY	= 135; // huhu
		
		_title.setX( cast Math.max( _currentX , 216 ) );
		_title.setY( cast Math.max( _currentY - 40, 0 ) );
		
		_container.setX( cast Math.max( _currentX , 216 ) );
		_container.setY( cast Math.max( _currentY , 0 ) );
	}

	
}